//
//  CYTabBarController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTabBarController.h"
#import "CYBuyViewController.h"
#import "CYUserViewController.h"
#import "CYOrderViewController.h"
#import "CYLoginViewController.h"
#import "CYTicketNoticeViewController.h"

#import "CYNavigationController.h"
@interface CYTabBarController ()<UITabBarControllerDelegate>
{
    UIImage *theme_image;
}

@end

@implementation CYTabBarController
+ (instancetype)sharedInstance{
    static CYTabBarController *shareManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[CYTabBarController alloc]init];
    });
    return shareManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self setupColor];
    [self setupViewController];
    //
    // Do any additional setup after loading the view.
}

/*设置tabbar字体颜色,背景颜色，  navigationbar的背景颜色 和字体颜色*/
- (void)setupColor{
    
    self.tabBar.tintColor = THEME_COLOR;
    UIView *color_view = [[UIView alloc]initWithFrame:self.tabBar.bounds];
    color_view.backgroundColor = [UIColor whiteColor];
    [self.tabBar insertSubview:color_view atIndex:0];
    self.tabBar.unselectedItemTintColor = RGBA(114, 115, 116, 1);
    
}

/*设置子视图控制器*/
- (void)setupViewController{
    
    NSArray *VC = @[@"CYBuyViewController", @"CYTicketNoticeViewController", @"CYOrderViewController", @"CYUserViewController"];
    NSArray *image = @[@"gciocn",@"kjicon",@"zdiocn",@"meiocn"];
    NSArray *selectedImage = @[@"gcrediocn",@"kjredicon",@"zdrediocn",@"merediocn"];
    
    NSMutableArray *controllers = [NSMutableArray array];
    
    for (int i = 0; i < VC.count; i ++) {
        
        Class VCclass = NSClassFromString(VC[i]);
        CYViewController *vc = [VCclass new];
        
        vc.tabBarItem = [[UITabBarItem alloc]initWithTitle:nil image:[[UIImage imageNamed:image[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImage[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        if (IS_IPHONE) {
            vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
        }
        CYNavigationController *navi = [[CYNavigationController alloc]initWithRootViewController:vc];
        [controllers addObject:navi];
        
    }
    
    self.viewControllers = controllers;
    
}

//如果未登录，注单和我的页面不能进入
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([viewController isEqual:self.viewControllers[2]] || [viewController isEqual:self.viewControllers[3]] ) {//注单和我的页面
        if (![CYCache readDataWithKey:@"username"] && ![CYCache readDataWithKey:@"token"]) {
            CYLoginViewController *loginVC = [[CYLoginViewController alloc]init];
            [tabBarController.selectedViewController pushViewController:loginVC animated:YES];
            return NO;
        }
    }
    return YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

